package edu.uwosh.cs342.project3;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class TrueFalse extends Activity implements OnCheckedChangeListener{
	
	int point = 0;
	String userAnswer = "";
	
	boolean[] submitArray;
	int questionPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.truefalse);
		
		Bundle extras = getIntent().getExtras();
		
		//  Get question and answer from extra
		String question = extras.getString("question");
		final String correctAnswer = extras.getString("correctAnswer");
		submitArray = extras.getBooleanArray("submitArray");
		questionPosition = extras.getInt("position");
		
		//  Setting up the question
		TextView questionTitle = (TextView) findViewById(R.id.trueFalseQuestion);
		questionTitle.setText(question);
		
		//  Setting up the radio group
		RadioGroup trueFalseGroup = (RadioGroup) findViewById(R.id.trueFalseRadioGroup);
		trueFalseGroup.clearCheck();
		trueFalseGroup.setOnCheckedChangeListener(this);
		
		//  Setting up the button
		Button trueFalseSubmitButton = (Button) findViewById(R.id.trueFalseSubmitButton);
		trueFalseSubmitButton.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v){
        		if(userAnswer.equals("")){
        			;
        		}
        		else
        		{
        			//  Return one point if student is correct
        			if (userAnswer.equals(correctAnswer)) {
        				point = 1;
        			}			
        			submitArray[questionPosition] = true;
        			finish();
        		}
        	}
        		
        });
	}

	public void onCheckedChanged(RadioGroup group, int checkedId) {
		
		//  Change user's answer when radio button is clicked
		switch(checkedId){
		case R.id.trueRadioButton:
			userAnswer = "True";
			break;
		case R.id.falseRadioButton:
			userAnswer = "False";
			break;
		}
		Toast.makeText(this, "Your answer changed to " + userAnswer, Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public void finish() {
		Intent data = new Intent();
		data.putExtra("point", point);
		data.putExtra("returnSubmit", submitArray);
		
		// Activity finished ok, return the data
		setResult(RESULT_OK, data);
		
		super.finish();	
	}
}